package com.ebring.shareEducation.service.impl;

import com.ebring.shareEducation.bean.Video;
import com.ebring.shareEducation.bean.VideoOrder;
import com.ebring.shareEducation.dao.VideoMapper;
import com.ebring.shareEducation.dao.VideoOrderMapper;
import com.ebring.shareEducation.service.VideoOrderService;
import com.ebring.shareEducation.utils.RspMsg;
import com.ebring.shareEducation.utils.UuidUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;


import java.text.SimpleDateFormat;
import java.util.Date;


/**
 * Created by Cola
 * Date  2018-12-20 10:21
 */
@Service
@Transactional
public class VideoOrderServiceImpl implements VideoOrderService {

    @Autowired
    VideoOrderMapper videoOrderMapper;

    @Autowired
    VideoMapper videoMapper;

    // 插入购买记录
    @Override
    public RspMsg<VideoOrder> insert(VideoOrder videoOrder) {
        RspMsg rspMsg = new RspMsg();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        videoOrder.setIdNo(UuidUtils.getUUID());
        videoOrder.setTranstime(simpleDateFormat.format(date));
        int insert = videoOrderMapper.insert(videoOrder);
        if (insert == 0) {
            rspMsg.setError();
            rspMsg.setMsg("购买记录失败！");
            return rspMsg;
        }

        rspMsg.setOK();
        return rspMsg;
    }

    // 查询购买记录
    @Override
    public RspMsg<VideoOrder> select(VideoOrder videoOrder) {
        RspMsg rspMsg = new RspMsg();
        Video video = videoMapper.selectByPrimaryKey(videoOrder.getVideoId());
        if (video.getMoney() == null) {
            rspMsg.setOK();
            return rspMsg;
        }
        Example example = new Example(VideoOrder.class);
        example.createCriteria().andEqualTo("buyId", videoOrder.getBuyId()).andEqualTo("videoId", videoOrder.getVideoId());
        VideoOrder videoOrder1 = videoOrderMapper.selectOneByExample(example);
        if (videoOrder1 == null) {
            rspMsg.setError();
            rspMsg.setMsg("未找到相关数据");
            return rspMsg;
        }

        rspMsg.setOK();
        return rspMsg;
    }
}
